<?php

namespace App\Http\Controllers\Home;

use App\Model\Article;
use App\Model\Cate;
use App\Model\Collect;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use phpDocumentor\Reflection\Types\Collection;
use  App\SMS\SendTemplateSMS;
use App\SMS\M3Result;
use App\Model\HomeUser;
use App\Model\Comment;

class IndexController extends CommonController
{
    public function index()
    {
        $cate_arts = Cate::where('cate_pid','!=','0')->with('article')->get();
        return view('home.index',compact('cate_arts'));
    }
    
    //收藏取消收藏

    public function collect(Request $request)
    {
        $uid = $request->input('uid');
        $artid = $request->input('artid');
        $act = $request->input('act');

        switch ($act){
            case 'add' :
                $collect = Collect::where([['uid','=',$uid],['art_id','=',$artid]])->get();
                //未被收藏
                if(!empty($collect)){
                    $res = Collect::create(['uid'=>$uid,'art_id'=>$artid]);
                    Article::where('art_id',$artid)->increment('art_collect');

                    if ($res){
                        return ['status'=>0,'message'=>'收藏成功!'];
                    }else{
                        return ['status'=>1,'message'=>'收藏失败!'];
                    }
                }else{
                    return ['status'=>0,'message'=>'已经收藏!'];
                }

                break;

            case 'remove' :
                  $res = Collect::where([['uid','=',$uid],['art_id','=',$artid]])->delete();
                Article::where('art_id',$artid)->decrement('art_collect');


                if ($res){
                    return ['status'=>0,'message'=>'取消成功!'];
                }else{
                    return ['status'=>1,'message'=>'取消失败!'];
                }

                break;

        }
    }

    //登录页面
    public function login()
    {
       return view('home.login');
    }

    //执行登录

    public function doLogin(Request $request)
    {
        $input = $request->except('_token');
        $input['user_pass'] = md5(md5($input['user_pass']));

        $member = HomeUser::where('phone',$input['user_name'])->first();

        if(!$member){
            $data = [
                'status'=>1,
                'message'=>'用户不存在'
            ];
            return $data;
        }

        if($input['user_pass'] !=($member->user_pass)){
            $data = [
                'status'=>1,
                'message'=>'密码错误'
            ];
            return $data;
        }

        session()->put(['user_home'=>$member]);

        return [
            'status'=>0,
            'message'=>'登录成功!'
        ];
    }
    //手机号祖册
    public function phoneRegister()
    {
        return view('home.pregister');
    }

    public function sendMessage(Request $request)
    {
        $phone = $request->input('phone');

        $code = rand(10000,99999);

        $arr = [$code,3];

        //调用容联云通讯接口

        $templateSMS = new SendTemplateSMS();
        $m3_result = new M3Result();

        $m3_result = $templateSMS->sendTemplateSMS($phone,$arr,1);

        if($m3_result->status==0){
            session()->put('phone_code',$code);
            return $m3_result->toJson();
        }else{
            return $m3_result->toJson();
        }

    }

    //执行注册用户

    public function doRegister(Request $request)
    {

        //判断验证码是否正确
        $code = $request->input('code');
        if ($code != session()->get('phone_code')){
            $data = [
                'status'=>1,
                'message'=>'验证码有误！'
            ];
            return $data;
        }
         $phone = $request->input('phone');
        $user_name = $request->input('user_name');


        $result = HomeUser::where('phone',$phone)->get();

            if($result){
                $data = [
                    'status'=>1,
                    'message'=>'手机号已被注册!！'
                ];
                return $data;
            }
         $user_pass = $request->input('user_pass');
         $user_pass = md5(md5($user_pass));
         $res = HomeUser::create(['user_name'=>$user_name,'phone'=>$phone,'user_pass'=>$user_pass]);

         if ($res){
             session()->put('home_user',$res);
             $data = [
                 'status'=>0,
                 'message'=>'注册成功！'
             ];
         }else{
             $data = [
                 'status'=>1,
                 'message'=>'注册失败！'
             ];
         }
         return $data;
    }


    public function detail(Request $request,$id)
    {
        \DB::table('article')
                ->where('art_id',$id)
                ->increment('art_view');

        $art = Article::with('cate')->where('art_id',$id)->first();

//        上一篇 下一篇
//        1 2 5  6 7 8
        $pre = Article::where('art_id','<',$id)->orderBy('art_id','desc')->first();
//        dd($pre);

        $next = Article::where('art_id','>',$id)->orderBy('art_id','asc')->first();


//        相关文章
        $similar = Article::where('cate_id',$art->cate_id)->take(4)->get();

//        文章评论
        $comment = Comment::where('post_id',$art->art_id)->get();
        return view('home.detail',compact('art','pre','next','similar','comment'));

    }




////    jiami
//
    public function jiami()
    {
        $str = 'Dyh123456';
        return MD5(md5($str));
    }
}
